library(tidyverse)

DATA                  <- read_csv(file.choose())
DATA                  <- DATA[order(DATA$COMPARISON.SHORT),]
DATA$COMPARISON.SHORT <- factor(DATA$COMPARISON.SHORT, levels = rev(unique(DATA$COMPARISON.SHORT)))

theme.z <- theme(
  axis.text.x.bottom = element_text(size = 15, color = "black", hjust = 0.5, vjust = 0.5, margin = margin(t = 5, r = 5, b = 5, l = 5)),
  axis.text.x.top    = element_text(size = 15, color = "black", hjust = 0.5, vjust = 0.5, margin = margin(t = 5, r = 5, b = 5, l = 5)),
  axis.text.y.left   = element_text(size = 15, color = "black", hjust = 1  , vjust = 0.5, margin = margin(t = 5, r = 10, b = 15, l = 10)),
  axis.text.y.right  = element_text(size = 15, color = "black", hjust = 0  , vjust = 0.5, margin = margin(t = 5, r = 10, b = 15, l = 10)),
  axis.ticks.x       = element_blank(), 
  axis.ticks.y       = element_blank(),
  axis.title.x       = element_text(size = 15, color = "black", hjust = 0.5, vjust = 0.5, margin = margin(t = 5, r = 5, b = 5, l = 5)),
  axis.title.y       = element_blank(), 
  axis.title.y.right = element_blank(),
  legend.position    = "none",
  panel.background   = element_rect(linewidth = 0.5, color = "black", linetype = "solid", fill = "gray90"),
  panel.border       = element_rect(linewidth = 2.0, color = "black", linetype = "solid", fill = NA),
  panel.grid.major.x = element_blank(),
  panel.grid.major.y = element_blank(),
  panel.grid.minor.x = element_blank(),
  panel.grid.minor.y = element_blank(),
  panel.spacing.x    = unit(1, "lines"),
  panel.spacing.y    = unit(1, "lines"),
  plot.background    = element_rect(fill = "white"),
  plot.caption       = element_text(size = 12, color = "black", hjust =   0, vjust = 0.5, margin = margin(t = 5 , r = 5, b = 5 , l = 5)),
  plot.margin        = unit(c(t = 5, r = 10, b = 5, l = 10), "pt"),
  plot.subtitle      = element_text(size = 15, color = "black", hjust = 0.5, vjust = 0.5, margin = margin(t = 5 , r = 5, b = 5 , l = 5)),
  plot.title         = element_text(size = 20, color = "black", hjust = 0.5, vjust = 0.5, margin = margin(t = 5 , r = 5, b = 5 , l = 5), face = "bold"),
  strip.background   = element_rect(linewidth = 1, color = "black", linetype = "solid", fill = "black"),
  strip.text.x       = element_text(size = 15, color = "white", hjust = 0.5, vjust = 0.5, margin = margin(t = 10, r = 5, b = 10, l = 5), face = "bold"),
  strip.text.y       = element_text(size = 15, color = "white", hjust = 0.5, vjust = 0.5, margin = margin(t = 4 , r = 4, b = 4 , l = 4), face = "bold")
)

X.MAX        <- 60
X.BUFFER.MIN <- 1
X.BUFFER.MAX <- 2.5

ggplot(DATA, aes(y = COMPARISON.SHORT, x = 100*PE, bg = COLOR)) +
  facet_wrap(vars(STEREOTYPE), nrow = 1, dir = "h") +
  geom_errorbar(orientation = "y", mapping = aes(xmin = 0 - X.BUFFER.MIN, xmax = X.MAX + X.BUFFER.MAX), width = 0   , linewidth = 7  , color = "gray80", alpha = 0.5) +
  geom_errorbar(orientation = "y", mapping = aes(xmin = 100*CILO        , xmax = 100*CIHI            ), width = 0.25, linewidth = 0.5, color = "black") +
  geom_point(size = 3.5, shape = 21, color = "black", stroke = 1.2) +
  scale_fill_manual(values = c("First listed group asked about first" = "black", "Second listed group asked about first" = "white")) + 
  scale_x_continuous(expand = c(0,0), limits = c(0 - X.BUFFER.MIN, X.MAX + X.BUFFER.MAX), breaks = seq(0, X.MAX + X.BUFFER.MAX, 10)) +
  scale_y_discrete(expand = c(0.065,0), position = "right") +
  labs(x = "Percentage") +
  theme.z

ggsave(file = "Stereotype ordering Figure 1.svg", width = 10, height = 4.5)
